package com.mm.common.pipeline;

/**
 * 流水线上待加工的产品，把数据和状态信息包装为产品
 */
public class Product{

    //数据
    private Object data;
    //状态信息
    private ProductCode productCode;

    private Product(Object data, ProductCode productCode) {
        this.data = data;
        this.productCode = productCode;
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获得一个正常产品
     * @Describe: TODO
     **/
    public static Product getNormal(Object data){
        return new Product(data, ProductCode.NORMAL);
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获得一个异常产品
     * @Describe: TODO
     **/
     public static Product getAbnormal(String message){
        return new Product(message, ProductCode.ABNORMAL);
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获取指定类型的产品数据
     * @Describe: TODO
     **/
     public <T> T getData(Class<T> retType) {
         //捕获类型转换的异常，并只返回一个null
         try {
             return (T)data;
         } catch (ClassCastException e) {
             e.printStackTrace();
             return null;
         }
     }

    public void setData(Object data) {
        this.data = data;
    }

    public ProductCode getProductCode() {
        return productCode;
    }

    public void setProductCode(ProductCode productCode) {
        this.productCode = productCode;
    }
}
